#include "stdlib.h"
#include "string.h"
#include <hidef.h> /* for EnableInterrupts macro */
#include "Headers/derivative.h" /* include peripheral declarations */
#include "Headers/define.h"
#include "Headers/GPIO.h"
#include "Headers/SPI.h"
#include "Headers/ICG.h"
#include "Headers/micelaneous.h"
#include "Headers/RF.h"
#include "Headers/time.h"
#include "Headers/ADC.h"
#include "Headers/regadera.h"
#include "Headers/232.h"

char regadera(void){
  char *command_rf, *command_rx, rta_fc = 0, error_adc;
  char *rs232_adc;
  word adc;
  rs232_adc = malloc(10);
  command_rf = malloc(50);
  command_rx = malloc(50);
  delay_ms(time_out);
  if(!RX_pkt_rf(command_rx)){
    error_adc = read_single_adc(5, &adc);
    if(!error_adc){
      adc &= 0xfffc;
      adc = adc >> 2;
      if(strcmp(command_rx, "go") == 0){
        time_out = 50;
        TX_pkt_rf("ok");
        TX_pkt_rf("ok");
        TX_pkt_rf("ok");
        TX_pkt_rf("ok");
        TX_pkt_rf("ok"); 
      }else{
        adc_regadera = adc & 0x00ff;
        adc_tinaco = *command_rx;
      }
      if(adc_regadera != 0) command_rf[0] = adc_regadera;
      else command_rf[0] = 1;
      command_rf[1] = '|';
      command_rf[2] = 'r';
      command_rf[3] = 'e';
      command_rf[4] = 'g';
      command_rf[5] = 'a';
      command_rf[6] = 'd';
      command_rf[7] = 'e';
      command_rf[8] = 'r';
      command_rf[9] = 'a';
      command_rf[10] = '|';
      command_rf[11] = 0;
      TX_pkt_rf(command_rf);
      TX_pkt_rf(command_rf);
      TX_pkt_rf(command_rf);
      TX_pkt_rf(command_rf);
      TX_pkt_rf(command_rf);
      //start byte rs232
      rs232_adc[0] = 0x02;
      //most significant 4 bits from adc trinaco
      rs232_adc[1] = adc_tinaco >> 4;
      if(rs232_adc[1] <= 9) rs232_adc[1] += 48;
      else rs232_adc[1] += 55;
      // lest significant 4 bits from adc tinaco
      rs232_adc[2] = adc_tinaco & 0x0f;
      if(rs232_adc[2] <= 9) rs232_adc[2] += 48;
      else rs232_adc[2] += 55;
      // separate charcter
      rs232_adc[3] = '|';
      // most significant 4 bits from adc in here
      rs232_adc[4] = adc_regadera >> 4;
      if(rs232_adc[4] <= 9) rs232_adc[4] += 48;
      else rs232_adc[4] += 55;
      // lest significant 4 bits from adc in here
      rs232_adc[5] = adc_regadera & 0x0f;
      if(rs232_adc[5] <= 9) rs232_adc[5] += 48;
      else rs232_adc[5] += 55;
      // stop byte
      rs232_adc[6] = 0x03;
      // this is for cut the string in the last value given
      rs232_adc[7] = 0x00;
      
      send_232(rs232_adc);
      if(adc_tinaco > 77) flag_tinaco = 1;
      if(flag_tinaco && time_out == 50){
        if(adc_tinaco < 65)flag_tinaco = 0;
        if(adc_regadera < 65)flag_regadera = 1;
        if(flag_regadera){
          if(adc_regadera > 77)flag_regadera = 0;
          ld1 = 1;
          ld2 = 1;
          ld3 = 1;
          ld4 = 1;
          if(count_buzzer < 15){
            PTDD_PTDD2 = 1;
            count_buzzer++;
          }else PTDD_PTDD2 = 0;
        }else{
          ld1 = 0;
          ld2 = 0;
          ld3 = 0;
          ld4 = 0;
          time_out = 1000;
          PTDD_PTDD2 = 1;
          count_buzzer = 0;
        }
      }else{
        ld1 = 0;
        ld2 = 0;
        ld3 = 0;
        ld4 = 0;
        time_out = 1000;
        PTDD_PTDD2 = 0;
        count_buzzer = 0;
      }
      
    }else rta_fc = 1;
  }
  free(rs232_adc);
  free(command_rf);
  free(command_rx);
  return rta_fc;
}